var userid_warn = "*\u60a8\u8f93\u5165\u7684Email\u5730\u5740\u9519\u8bef";// 
var pwd_warn = "*\u5bc6\u7801\u957f\u5ea6\u5fc5\u987b\u57284-20\u4e4b\u95f4";// 
var repwd_warn = "*\u60a8\u4e24\u6b21\u8f93\u5165\u7684\u5bc6\u7801\u4e0d\u4e00\u81f4"; //
var submit_warn= "\u8bf7\u6b63\u786e\u586b\u5199\u6ce8\u518c\u4fe1\u606f";//
var chkcode_warn="\u8bf7\u8f93\u5165\u56fe\u4e2d\u6240\u663e\u793a4\u4f4d\u9a8c\u8bc1\u7801";//
var name_warn="\u6635\u79f0\u957f\u5ea6\u8bf7\u4e0d\u8981\u8d85\u8fc740\u4e2a\u5b57\u7b26\u621620\u4e2a\u6c49\u5b57";
function getCookie(name) {
	var cookie_start = document.cookie.indexOf(name);
	var cookie_end = document.cookie.indexOf(";", cookie_start);
	return cookie_start == -1 ? '' : unescape(document.cookie.substring(
			cookie_start + name.length + 1,
			(cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}

function setCookie(name, value) {
	document.cookie = name + "=" + escape(value) + ";path=/";
}
$(document).ready(function() {

	x = getCookie("user");
	if (x == "") {
		showNologin();
	} else {
		showLogined();
	}
});

function showNologin() {
	$("#navi").html("");
	$("#navi")
			.append(
					"<div><a href='jsp/index.jsp'><img src='img/rplogo.jpg' align=left style='border-color:rgb(14, 96, 159) ;padding:0; border:0;' width=152 height=35/></a></div><div style='text-align: right;margin-right:1px;padding-top:8px;'><a class='top'	href='javascript:showLoginWin()'>登录</a><a class='top'	href='javascript:showRegWin()'>没有帐号?马上注册</a></div><a name='pagetop' href='#pagetop'></a>");
}

function showLogined() {
	$("#navi").html("");
	$("#navi")
			.append(
					"<div><a href='jsp/index.jsp'><img src='img/rplogo.jpg' align=left style='border-color:rgb(14, 96, 159) ;padding:0; border:0;' width=152 height=35/></a></div><div style='text-align: right;margin-right:1px;padding-top:8px;'> <a href='jsp/newitem.jsp' class='top'>+创建人物</a><a href='jsp/commented.jsp' class='top'>我评价过的</a><a class='top'	href='javascript:exitsys()'>退出</a> </div><a name='pagetop' href='#pagetop'></a>");
}

function exitsys() {
	setCookie("user", "");
	showNologin();
}

function login() {
	var userid = $("#userid")[0].value;
	var password = $("#password")[0].value;
	userid = $.trim(userid);
	userid = userid.toLowerCase();

	if (1 > userid.length || 1 > password.length) {
		loginwarn("\u8bf7\u8f93\u5165\u6b63\u786e\u7684\u90ae\u7bb1\u540d\u548c\u5bc6\u7801");
		return;
	}

	var data = "email=" + userid + "&pwd=" + password;
	$.ajax( {
		type : "post",
		url : "Login",
		dataType : "json",
		data : data,
		success : function(rsp) {
			loginCallback(rsp);
		}
	});
}

function loginCallback(rsp) {
	if (rsp["msg"] == 0) {
		closeTop();
		showLogined();
	} else {
		loginwarn(rsp["msg"]);
	}
}

function loginwarn(warn_msg) {
	$("#logtip").empty();
	$("#logtip").append("<label style='color:red;'>" + warn_msg + "</label>");
}

function showLoginWin() {
	
	var cont = "<div style='padding-left:60px;' text-align:center;><table style='text-align:center;border-spacing:10px;'>"
		+"<tr><td></td><td style='text-align:left; font-size:38px; color:rgb(14,96,158);font-weight:800;'>马上开始</td></tr>"
	+"<tr><td></td><td id='logtip'></td></tr>"
			+ "<tr><td style='text-align:left; font-size:16px; color:rgb(14,96,158); font-weight:800;'>邮箱:</td><td><input type='text' class='tbox' placeholder='邮箱' name='userid' id='userid' maxlength='50' /></td></tr>"
			+ "<tr><td style='text-align:left; font-size:16px; color:rgb(14,96,158); font-weight:800;'>密码:</td><td><input type='password' class='tbox' placeholder='密码' name='password' id='password' maxlength='50' /></td></tr>"
			+ "<tr><td></td><td><img onclick='login()' src='img/lgbtn.jpg' class='cursor' alt='登录' width='141' height='51' /></td></tr>"
			+"<tr><td></td><td style='color:rgb(109,137,168);'>没有帐号？马上 <a href='javascript:showRegWin();'>注册</a></td></tr></table></div>";
	showTop(cont, 700, 448,100);
}

function showTop(content, w, h,t) {
	// var bH=$("#middle").height();
	var ph=$(document).height();
	$("#fullBg").css( {
		display : "block",
		height:ph
	});
	
	
	var l=(window.screen.width-700)/2;
	if(window.screen.width>w)
		l=(window.screen.width-w)/2;
	else
		l=0;
	
	$("#msg").css( {
		top : t,
		left : l,
		height : h,
		width : w,
		display : "block"
	});
	$("#ctt").empty();
	$("#ctt").append(content);
}

//关闭
function closeTop() {
	$("#fullBg").css("display", "none");
	$("#msg").css("display", "none");
}


function showRegWin(){
	var content = "<div  style='padding-left:60px;' text-align:center;><table style='text-align:center;'><tr><td></td><td id='regtip' colspan='2'></td></tr>"
		+ "<tr><td style='text-align:left; font-size:16px; color:rgb(14,96,158); font-weight:800;'>邮箱:</td><td><input type='text' placeholder='\u6ce8\u518c\u90ae\u7bb1' class='tbox' name='email' id='email' maxlength='50' /></td></tr>"
		+ "<tr><td style='text-align:left; font-size:16px; color:rgb(14,96,158); font-weight:800;'>昵称:</td><td><input type='text' placeholder='\u6635\u79f0' class='tbox' name='name' id='name' maxlength='50' /></td></tr>"
		+ "<tr><td style='text-align:left; font-size:16px; color:rgb(14,96,158); font-weight:800;'>密码:</td><td><input type='password' class='tbox' placeholder='\u5bc6\u7801' name='rpassword' id='rpassword' maxlength='50' /></td></tr>"
		+ "<tr><td style='text-align:left; font-size:16px; color:rgb(14,96,158); font-weight:800;'>密码确认:</td><td><input type='password' class='tbox' placeholder='\u786e\u8ba4\u5bc6\u7801' name='repassword' id='repassword' maxlength='50'/></td></tr>"
		+"<tr><td></td><td><img id='codeimg' src='jsp/verifycode.jsp' onclick='javascript:verifycode();' alt='刷新' width=101 height=41> <span class='help'> <a 	href='javascript:verifycode();'>看不清，换一张</a> </span></td></tr>"
		+ "<tr><td></td><td><input type='text' class='tbox' placeholder='\u9a8c\u8bc1\u7801' name='verifycode' id='verifycode' maxlength='4' /></td></tr>"
		+ "<tr><td></td><td><img  src='img/regbtn.jpg' onclick='register()' class='cursor' alt='注册'  width=140 height=50/> </td></tr></table></div>";
	showTop(content, 700, 448,100);
}


function register(){
	var email = $("#email")[0].value;
	var password = $("#rpassword")[0].value;
	var name = $("#name")[0].value;
	var verifycode=$("#verifycode")[0].value;
	email = $.trim(email);
	email = email.toLowerCase();
	if( emailvalid() && namevalid() && pwdvalid() && repwdvalid()&& chkcodevalid() )
	{
	var data = "email=" + email + "&pwd=" + password+"&name="+name+"&vcode="+verifycode;
	$.ajax( {
		type : "get",
		url : "Register",
		dataType : "json",
		data : data,
		success : function(rsp) {
			regCallback(rsp);
		}
	});
	}
}


function regCallback(rsp){
	if (rsp["msg"] == 0) {
		closeTop();
		showLogined();
	} else {
		regwarn(rsp["msg"]);
		verifycode();
	}
}
function regwarn(warn_msg) {
	$("#regtip").empty();
	$("#regtip").append("<label style='color:red;'>" + warn_msg + "</label>");
}

function namevalid(){
	var name = $("#name")[0].value;
	if (name.length>0 && name.length<40){
		return true;
	}
	else{
		regwarn(name_warn);
		return false;
	}
}

function chkcodevalid() {
	var jqueryobj =$.trim($("#verifycode")[0].value);
	if (jqueryobj.length != 4) {
		regwarn(chkcode_warn);
		return false;
	} else {
		regwarn("");
		return true;
	}
}

function repwdvalid() {	
	pwd = $("#rpassword")[0].value;
	var repwd = $("#repassword")[0].value;
	if (pwd == repwd) {
		regwarn("");
		return true;
	} else {
		regwarn(repwd_warn);
		return false;
	}
}

function pwdvalid() {
	if (pwd_validator()) {
		regwarn(pwd_warn);
		return false;
	} else {
		regwarn("");
		return true;
	}
}

function pwd_validator() {
	// return true;
	pwd = $("#rpassword")[0].value;
	if (4 > pwd.length || 20 < pwd.length) {
		return true;
	} else {
		return false;
	}
}

function emailvalid() {
	var flag = true;
	var email = $.trim($("#email")[0].value.toLowerCase());
	$("#email")[0].value = email;
	var pattern =/^([a-z0-9])(([\-.]|[_]+)?([a-z0-9]+))*(@)([a-z0-9])((([-]+)?([a-z0-9]+))?)*((.[a-z]{2,3})?(.[a-z]{2,6}))$/;
	
	if (email.indexOf("@") > 0 ) {
		flag = pattern.test(email);
		if (flag) {
			regwarn();
			return true;
		} else {
			regwarn(userid_warn);
			return false;
		}
	} else {
		regwarn(userid_warn);
		return false;
	}
}

function verifycode(){	
	  var jqueryobj=$("#codeimg")[0]; 	  
	  jqueryobj.src=jqueryobj.src+"?";
}


function antiJs(data){
	var sv=["<",">","/","\\","?","!","\"","'"];
	for(var s in sv){
	while(data.indexOf(sv[s])>=0)
		data=data.replace(sv[s],"");
	}
	return data;
}
